using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowFinder
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings : System.Windows.Forms.Form
	{
        private System.Windows.Forms.Button OkButton;
        private TrackBar OpacityBar;
        private GroupBox groupBox2;
        private ComboBox MainCombo;
        private CheckedListBox ModifierList;
        private GroupBox groupBox1;
        private Label PercentLabel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
        private System.ComponentModel.Container components = null;

		public Settings()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.ModifierList.Validating += new CancelEventHandler(ValidateModifiers);

			foreach (KeyCodes.VirtualKey x in KeyCodes.VirtualKey.GetValues(typeof(KeyCodes.VirtualKey)))
			{
				MainCombo.Items.Add(x.serialize());
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.OkButton = new System.Windows.Forms.Button();
            this.OpacityBar = new System.Windows.Forms.TrackBar();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.MainCombo = new System.Windows.Forms.ComboBox();
            this.ModifierList = new System.Windows.Forms.CheckedListBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.PercentLabel = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.OpacityBar)).BeginInit();
            this.groupBox2.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // OkButton
            // 
            this.OkButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.OkButton.Location = new System.Drawing.Point(164, 151);
            this.OkButton.Name = "OkButton";
            this.OkButton.Size = new System.Drawing.Size(75, 23);
            this.OkButton.TabIndex = 1;
            this.OkButton.Text = "Accept";
            // 
            // OpacityBar
            // 
            this.OpacityBar.Dock = System.Windows.Forms.DockStyle.Left;
            this.OpacityBar.Location = new System.Drawing.Point(3, 16);
            this.OpacityBar.Maximum = 100;
            this.OpacityBar.Minimum = 20;
            this.OpacityBar.Name = "OpacityBar";
            this.OpacityBar.Orientation = System.Windows.Forms.Orientation.Vertical;
            this.OpacityBar.Size = new System.Drawing.Size(42, 104);
            this.OpacityBar.TabIndex = 0;
            this.OpacityBar.TickFrequency = 5;
            this.OpacityBar.Value = 100;
            this.OpacityBar.Scroll += new System.EventHandler(this.OpacityBar_Scroll);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.MainCombo);
            this.groupBox2.Controls.Add(this.ModifierList);
            this.groupBox2.Location = new System.Drawing.Point(12, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(121, 123);
            this.groupBox2.TabIndex = 10;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Hotkey";
            // 
            // MainCombo
            // 
            this.MainCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.MainCombo.DropDownWidth = 40;
            this.MainCombo.Location = new System.Drawing.Point(11, 92);
            this.MainCombo.Name = "MainCombo";
            this.MainCombo.Size = new System.Drawing.Size(96, 21);
            this.MainCombo.TabIndex = 9;
            // 
            // ModifierList
            // 
            this.ModifierList.CheckOnClick = true;
            this.ModifierList.Items.AddRange(new object[] {
            "Alt",
            "Control",
            "Shift",
            "Windows Key"});
            this.ModifierList.Location = new System.Drawing.Point(11, 22);
            this.ModifierList.Name = "ModifierList";
            this.ModifierList.Size = new System.Drawing.Size(96, 64);
            this.ModifierList.TabIndex = 8;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.PercentLabel);
            this.groupBox1.Controls.Add(this.OpacityBar);
            this.groupBox1.Location = new System.Drawing.Point(139, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(105, 123);
            this.groupBox1.TabIndex = 10;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Opacity";
            // 
            // PercentLabel
            // 
            this.PercentLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.PercentLabel.AutoSize = true;
            this.PercentLabel.Location = new System.Drawing.Point(56, 56);
            this.PercentLabel.Name = "PercentLabel";
            this.PercentLabel.Size = new System.Drawing.Size(33, 13);
            this.PercentLabel.TabIndex = 1;
            this.PercentLabel.Text = "100%";
            // 
            // Settings
            // 
            this.AcceptButton = this.OkButton;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(251, 186);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.OkButton);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Settings";
            this.Text = "Settings";
            ((System.ComponentModel.ISupportInitialize)(this.OpacityBar)).EndInit();
            this.groupBox2.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		private void ValidateModifiers(object sender, CancelEventArgs e)
		{
			if (ModifierList.CheckedItems.Count == 0)
			{
				e.Cancel = true;
				MessageBox.Show("You must select at least one Hot Key Modifier","Hot Key Modifiers");
			}
		}

		public KeyCodes.VirtualKey HideKey
		{
			get
			{
				return KeyCodes.deserializeKey((String)MainCombo.SelectedItem);
			}
			set
			{
				MainCombo.SelectedItem = value.serialize();
			}
		}

		new public double Opacity
		{
			get
			{
				return Convert.ToDouble(OpacityBar.Value)/100;
			}
			set
			{
				OpacityBar.Value = (int)(value*100);
				base.Opacity = value;
			}
		}

		public KeyCodes.Modifier HideModifiers
		{
			get
			{
				KeyCodes.Modifier mods = 0;

				foreach(string modKey in ModifierList.CheckedItems)
				{
					switch(modKey)
					{
						case "Alt":
							mods = mods | KeyCodes.Modifier.ALT;
							break;
						case "Shift":
							mods = mods | KeyCodes.Modifier.SHIFT;
							break;
						case "Control":
							mods = mods | KeyCodes.Modifier.CONTROL;
							break;
						case "Windows Key":
							mods = mods | KeyCodes.Modifier.WINDOWS;
							break;
					}
				}

				return mods;
			}
			set
			{
                this.ModifierList.SetItemChecked(3, KeyCodes.Modifier.WINDOWS.isSet(value));
                this.ModifierList.SetItemChecked(2, KeyCodes.Modifier.SHIFT.isSet(value));
                this.ModifierList.SetItemChecked(1, KeyCodes.Modifier.CONTROL.isSet(value));
                this.ModifierList.SetItemChecked(0, KeyCodes.Modifier.ALT.isSet(value));
			}
		}

        private void OpacityBar_Scroll(object sender, EventArgs e)
        {
            PercentLabel.Text = OpacityBar.Value + "%";
        }
	}
}
